﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Drawing;

namespace PHTShop
{
    public partial class WebForm8 : basePageUser
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
          
            switch (Session["languages"].ToString().ToLower())
            {
                case "france":
                    Page.Title = "Contactez";
                    break;
                case "germany":
                    Page.Title = "Kontakt";
                    break;
                default:
                    Page.Title = "Contact";
                    break;
            }
        }
        
        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        protected void btnsend_click(object sender, EventArgs e)
        {
            this.Labelname.Text = "";
            this.Labelemail.Text = "";
            this.Labelmessage.Text = "";
            if (check(this.txtname.Value) == true)
            {
                this.Labelname.Text = "not null";
            }
            else if (check(this.txtemail.Value) == true)
            {
                this.Labelemail.Text = "not null";
            }
            else if (check(this.txtmessage.Value) == true)
            {
                this.Labelmessage.Text = "not null";
            }
            else
            {
                Setting email = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Email"));
                Setting password = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Password"));
                Setting smtp = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("Smtp"));
                Setting port = this.dc.Settings.Single(t => t.type.Equals("Email") && t.name.Equals("port"));
                try
                {
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress(email.value);
                    msg.To.Add(email.value);
                    msg.Subject = this.txtname.Value;
                    msg.Body = "Name : " + this.txtname.Value + "\nEmail : " + this.txtemail.Value + "\nPhone : " + this.txtphone.Value + "\nCompany : " + this.txtcompany.Value + "\nMessage : " + this.txtmessage.Value;

                    SmtpClient sc = new SmtpClient(smtp.value);
                    sc.Port = Convert.ToInt32(port.value);
                    sc.Credentials = new NetworkCredential(email.value, password.value);
                    sc.EnableSsl = true;
                    sc.Send(msg);

                    this.Labelsuccess.Text = "Thanks you !";
                }
                catch
                {
                    this.Labelsuccess.Text = "Send mail is false !";
                }
            }
        }

        private bool check(string str)
        {
            return String.IsNullOrEmpty(str.Trim());
        }
    }
}